﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Telecom.Data;
using Telecom.Service.EntityCustom;

namespace Telecom.Service
{
    public class PortSer
    {
        private readonly EntitiesTelecom dbContext = new EntitiesTelecom();

        public PortSer()
        {
            dbContext.Configuration.LazyLoadingEnabled = false;
            dbContext.Configuration.ProxyCreationEnabled = false;
        }

        public bool addorUpdate(PORT entity)
        {
            try
            {
                if (entity.ID == 0) dbContext.PORT.Add(entity);
                else dbContext.Entry(entity).State = System.Data.EntityState.Modified;
                dbContext.SaveChanges();
                return true;
            }
            catch (Exception e)
            {
                return false;
            }
        }

        public PORT getPortById(int PortId)
        {
            try
            {
                return dbContext.PORT.Where(p => p.ID == PortId || PortId == 0).First();
            }
            catch (Exception e)
            {
                PORT _listRe = new PORT();
                return _listRe;
            }
        }


        public List<PORT> getList(int CauHinhId)
        {
            try
            {
                return dbContext.PORT.Where(p => p.CAUHINHID == CauHinhId || CauHinhId == 0).ToList();
            }
            catch (Exception e)
            {
                List<PORT> _listRe = new List<PORT>();
                return _listRe;
            }
        }

        public List<PORT> getListCauHinhArray(int[] CauHinhId)
        {
            try
            {
                return dbContext.PORT.Where(p => CauHinhId.Contains(p.CAUHINHID.Value)).ToList();
            }
            catch (Exception e)
            {
                List<PORT> _listRe = new List<PORT>();
                return _listRe;
            }
        }

        public List<PORT> getListProcess(int CauHinhId)
        {
            try
            {
                return dbContext.PORT.Where(p => p.CAUHINHID == CauHinhId && (p.TRANGTHAI == 1 || p.TRANGTHAI == 2)).ToList();
            }
            catch (Exception e)
            {
                List<PORT> _listRe = new List<PORT>();
                return _listRe;
            }
        }

        public List<PORT> getListProcess_dau_noi(int CauHinhId)
        {
            try
            {
                return dbContext.PORT.Where(p => p.CAUHINHID == CauHinhId  && (p.TRANGTHAI == 1 || p.TRANGTHAI == 2)).ToList();
            }
            catch (Exception e)
            {
                List<PORT> _listRe = new List<PORT>();
                return _listRe;
            }
        }

        public PORT GetPortById(int id)
        {
            try
            {
                return dbContext.PORT.Where(p => p.ID == id).First();
            }
            catch (Exception e)
            {
                PORT _listRe = new PORT();
                return _listRe;
            }
        }

        public TRANGTHAI getTTbyId(decimal ttId)
        {
            try
            {
                return dbContext.TRANGTHAI.Where(p => p.TTID == ttId).First();
            }
            catch (Exception e)
            {
                TRANGTHAI _listRe = new TRANGTHAI();
                return _listRe;
            }
        }



        public List<JsonJtable> getListTT()
        {
            try
            {
                List<JsonJtable> returnL = new List<JsonJtable>();
                var list = dbContext.TRANGTHAI.ToList();
                foreach (var item in list)
                {
                    JsonJtable _json = new JsonJtable();
                    _json.DisplayText = item.TRANGTHAI1;
                    _json.Value = item.TTID;
                    returnL.Add(_json);
                }

                return returnL;

            }
            catch (Exception e)
            {
                List<JsonJtable> _listRe = new List<JsonJtable>();
                return _listRe;
            }
        }
    }
}
